package org.sn.jdish.example.stack;

/**
 * 两个栈实现一个队列
 * 
 * @author snzigod@hotmail.com
 *
 */
public class Stack2Queue {

	private Stack stack1 = new Stack();
	private Stack stack2 = new Stack();

	private int size = 0;

	public void enQueue(Object data) {
		stack1.push(data);
		size++;
	}

	public Object deQueue() {
		if (stack2.empty()) {
			while (!stack1.empty()) {
				stack2.push(stack1.pop());
			}
		}
		if (stack2.empty()) {
			return null;
		}
		size--;
		return stack2.pop();
	}

	public boolean empty() {
		return size == 0;
	}

	public static void main(String[] args) {
		Stack2Queue queue = new Stack2Queue();
		for (int i = 0; i < 5; i++) {
			queue.enQueue(i);
		}
		while (!queue.empty()) {
			System.out.println(queue.deQueue());
		}
	}
}
